<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="园区名称" placeholder="请输入园区名称" prop="name">
						<el-input v-model="form.name" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="总面积" prop="total_area">
						<el-input v-model="form.total_area" placeholder="亩/平米" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="省" prop="province_id">
						<!-- <el-input v-model="form.province_id" clearable maxlength="80"></el-input> -->
						<el-select v-model="form.province_id" placeholder="请选择省份" @change="onSelectProvince" clearable filterable style="width: 100%">
							<el-option
							v-for="item in provinceData"
							:key="item.id"
							:label="item.city_name"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="租金定价（聚合）" prop="rental_pricing">
						<el-input v-model="form.rental_pricing" placeholder="元/平米/天" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="市" prop="city_id">
						<!-- <el-input v-model="form.city_id" clearable maxlength="80"></el-input> -->
						<el-select v-model="form.city_id" placeholder="请选择城市" @change="onSelectCity" clearable filterable style="width: 100%">
							<el-option
							v-for="item in cityData"
							:key="item.id"
							:label="item.city_name"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物业费（聚合）" prop="strata_fees">
						<el-input v-model="form.strata_fees" placeholder="元/平米/天" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="区县" prop="district_id">
						<!-- <el-input v-model="form.district_id" clearable maxlength="80"></el-input> -->
						<el-select v-model="form.district_id" placeholder="请选择区县" @change="onSelectDistrict" clearable filterable style="width: 100%">
							<el-option
							v-for="item in districtData"
							:key="item.id"
							:label="item.city_name"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="企业数量（聚合）" prop="enterprises">
						<el-input v-model="form.enterprises" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="园区类别" prop="park_category">
						<sc-select v-model="form.park_category" :params="{code:'supervisionLevel'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否竞品" prop="competition">
						<sc-select v-model="form.competition" :params="{code:'supervisionLevel'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="产品定位" prop="industry_positioning">
				<el-input v-model="form.industry_positioning" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-form-item label="园区介绍" >
				<sc-editor v-model="form.introduce" placeholder="请输入" :templates="templates" :height="400" clearable  style="width: 100%;"></sc-editor>
			</el-form-item>
			<!-- <msg-box-check-box v-model:msgType="form.msgType" code="supervision"></msg-box-check-box> -->
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
import {defineAsyncComponent} from "vue";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		msgBoxCheckBox,
		selectUserDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			originProvinceCityDistrictData:[],
			allProvinceCityDistrictData:[],
			provinceData: [],
			cityData: [],
			districtData: [],
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			mode: "add",
			templates: [],
			fileList:[],
			userList:[],
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '产业园区信息',
				edit: '产业园区档案',
			},
			form: {
				id:'',
				name:'',
				total_area: '',
				province_id: '',
				rental_pricing: '',
				city_id:"",
				strata_fees:"",
				district_id:'',
				enterprises:'',
				park_category: '',
				competition: '',
				industry_positioning:'',
				image_upload:'',
				annex_upload:'',
				introduce:''
			},
			rules: {
				name: [
					{required: true, message: '园区名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getParkList();
		this.getProvinceList();
		// this.getAreaList();
	},
	methods: {
		//根据省更新市数据
		async onSelectProvince(e){
			const isNormalProvince = this.provinceData.filter(element=>{return element.id==e})[0].city_code==''
			if(isNormalProvince){
				let res = await this.$API.attractinvestment.common.getCityDistrictList.get({id:e});
				this.cityData = res.data
			}else{
				this.cityData = this.provinceData.filter(element=>{return element.id==e})
			}
			this.cityData.unshift({'city_name':'请选择', 'id':0})
			this.form.city_id = 0
			this.form.district_id = 0
		},
		//根据市更新区数据
		async onSelectCity(e){
			let res = await this.$API.attractinvestment.common.getCityDistrictList.get({id:e});
			this.districtData = res.data
			this.districtData.unshift({'city_name':'请选择', 'id':0})
			this.form.district_id = 0
		},
		//根据省更新市数据
		async fetchCity(){
			const e = this.form.province_id
			if(this.form.province_id==''){
				return false
			}else{
				const isNormalProvince = this.provinceData.filter(element=>{return element.id==e})[0].city_code==''
				if(isNormalProvince){
					let res = await this.$API.attractinvestment.common.getCityDistrictList.get({id:e});
					this.cityData = res.data
				}else{
					this.cityData = this.provinceData.filter(element=>{return element.id==e})
				}
				this.cityData.unshift({'city_name':'请选择', 'id':0})
			}
		},
		//根据市更新区数据
		async fetchDistrict(){
			const e = this.form.city_id
			if(this.form.city_id==''){
				return false
			}else{
				let res = await this.$API.attractinvestment.common.getCityDistrictList.get({id:e});
				this.districtData = res.data
				this.districtData.unshift({'city_name':'请选择', 'id':0})
			}
		},
		//筛选省份
		onSelectDistrict(e){
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		async getParkList(){
			let res = await this.$API.attractinvestment.park.getParkList.get();
			if (res.code == 200) {
				this.userList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		// //获取省市区数据
		// async getProvinceCityDistrictList(){
		// 	let res = await this.$API.attractinvestment.common.getProvinceCityDistrictList.get();
		// 	if (res.code == 200) {
		// 		this.originProvinceCityDistrictData = res.data;
		// 		this.allProvinceCityDistrictData = JSON.parse(JSON.stringify(this.originProvinceCityDistrictData));
		// 		this.allProvinceCityDistrictData.forEach(element => {
		// 			element.my_value = element.id + '_' + element.pid + '_' + element.city_code
		// 		});
		// 		this.provinceData = this.allProvinceCityDistrictData.filter(e=>{
		// 			return e.pid ==0;
		// 		})
		// 	} else {
		// 		this.$alert(res.message, "提示", {type: 'error'})
		// 	}
		// },
		//获取省数据
		async getProvinceList(){
			let res = await this.$API.attractinvestment.common.getProvinceList.get();
			if (res.code == 200) {
				this.provinceData = res.data
				this.fetchCity(this.form.province_id)
				this.fetchDistrict(this.form.city_id)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					delete this.form.rental_pricing
					delete this.form.strata_fees
					delete this.form.total_area
					if (this.mode == "add") {
						res = await this.$API.attractinvestment.park.insertPark.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.attractinvestment.park.updatePark.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.total_area = data.total_area
			this.form.province_id = data.province_id
			this.form.rental_pricing = data.rental_pricing
			this.form.city_id = data.city_id
			this.form.strata_fees = data.strata_feesww
			this.form.district_id = data.district_id
			this.form.enterprises = data.enterprises
			this.form.park_category = data.park_category
			this.form.competition = data.competition
			this.form.industry_positioning = data.industry_positioning
			this.form.image_upload = data.image_upload
			this.form.annex_upload = data.annex_upload
			this.form.introduce = data.introduce
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
